// components/MusicList/MusicList.js
import { myRequest } from '../../utils/request';
import api from '../../utils/api'
import Cache from '../../utils/cache';
import Tool from '../../utils/tool';
const app = getApp();

Component({
  /**
   * 组件的属性列表
   * 由组件外部传入的数据 等同于vue中的props
   */
  properties: {
    type: {
      type: String,
      value: ''  // 1：专辑  2：歌单  3：歌手
    },
    searchId: {
      type: String,
      value: ''
    }
  },

  observers: {
    'searchId, type': function (searchId, type) {
      if(type === '1') {
        // 专辑
        this.getAlbumById(searchId);
        this.getMusicListByAlbumId(searchId);
        // 浏览数增加
        this.addAlbumView(searchId);
      } else if(type === '2') {
        // 歌单
        this.getMusicMenuById(searchId);
        this.getMusicListByMusicMenuId(searchId);
        this.getIsMySelfMusicMenu(searchId);
        // 浏览数增加
        this.addMusicMenuView(searchId);
      } else if (type === '3') {
        // 歌手
        this.getSingerById(searchId);
        this.getMusicListBySingerId(searchId);
        // 浏览数增加
        this.addSingerView(searchId);
      }
  
    }
  },

  pageLifetimes: {
    show() {
      this.setData({
        playingMusicId: app.getPlayMusicId()
      })
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 当前机型的状态栏高度
    barHeight: 0,
    musicList: [],
    album: {},
    musicMenu: {},
    singer: {},
    isMySelfMusicMenu: false, // 判断是否是当前登录用户的歌单
    playingMusicId: '',
    showConfirmModel: false,
    confirmText: '确认要删除此歌单音乐？',
    removeMusicId: ''
  },

  lifetimes: {
    attached: async function () {
      // 获取当前机型的状态栏高度
      wx.getSystemInfo({
        success: e => {
          // e.statusBarHeight; //状态栏高
          // console.log(e.statusBarHeight);
          this.setData({
            barHeight: e.statusBarHeight
          })
        }
      })


    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
   // 增加歌单浏览量
   async addMusicMenuView(searchId) {
    const res = await myRequest({
      url: '/app/music/menu/addView',
      method: 'POST',
      data: {id: searchId}
    });
   },
   // 判断歌单是否是当前登录用户的
   async getIsMySelfMusicMenu(searchId) {
      const loginUser = Cache.getCache(getApp().globalData.SESSION_KEY_LOGIN_USER);
      const res = await myRequest({
        url: '/app/music/menu/isMySelf',
        method: 'POST',
        data: {id: searchId, token: loginUser}
      });
      if(res.data.code === 0) {
        this.setData({isMySelfMusicMenu: res.data.data});
      }
   },
   // 根据id获取歌单数据
   async getMusicMenuById(searchId) {
      const res = await myRequest({
        url: '/app/music/menu/getById',
        method: 'POST',
        data: {id: searchId}
      });
      if(res.data.code === 0) {
        let result = res.data.data;
        result.coverPhoto = api.BASE_URL + '/photo/view?filename=' + result.coverPhoto;
        result.headPic = api.BASE_URL + '/photo/view?filename=' + result.headPic;
        this.setData({musicMenu: result});
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
   },
   // 根据歌单id获取音乐数据
   async getMusicListByMusicMenuId(searchId) {
      const res = await myRequest({
        url: '/app/music/getByMusicMenuId',
        method: 'POST',
        data: {id: searchId}
      });
      if(res.data.code === 0) {
        let result = res.data.data;
        this.setData({musicList: result});
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
   },
    // 根据歌手id获取音乐数据
    async getMusicListBySingerId(searchId) {
      const res = await myRequest({
        url: '/app/music/getBySingerId',
        method: 'POST',
        data: {singerId: searchId}
      });
      if(res.data.code === 0) {
        let result = res.data.data;
        this.setData({musicList: result});
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
   },
   cancelShowConfirmModel() {
    this.setData({showConfirmModel: false});
   },
  openShowRemoveMusicMenuItem(event) {
      const data = event.currentTarget.dataset
      this.setData({showConfirmModel: true, removeMusicId: data.id});
   },
   async confirmShowModel() {
      this.setData({showConfirmModel: false});
      let musicMenuId = this.properties.searchId;
      let musicId = this.data.removeMusicId;
      const res = await myRequest({
        url: '/app/music/menu/removeItem',
        method: 'POST',
        data: {musicId: musicId, musicMenuId: musicMenuId}
      });
      if(res.data.code === 0) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success'
        })
        this.getMusicListByMusicMenuId(musicMenuId);
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
   },
   // 根据专辑id获取音乐数据
   async getMusicListByAlbumId(searchId) {
      const res = await myRequest({
        url: '/app/music/getByAlbumId',
        method: 'POST',
        data: {albumId: searchId}
      });
      if(res.data.code === 0) {
          let result = res.data.data;
          this.setData({musicList: result});
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
   },
   // 增加专辑浏览量
   async addAlbumView(searchId) {
      const res = await myRequest({
        url: '/app/album/addView',
        method: 'POST',
        data: {id: searchId}
      });
   },
   // 根据id获取歌手数据
   async getSingerById(searchId) {
    const res = await myRequest({
      url: '/app/singer/getById',
      method: 'POST',
      data: {id: searchId}
    });
    if(res.data.code === 0) {
        let result = res.data.data;
        result.headPic = api.BASE_URL + '/photo/view?filename=' + result.headPic;
        this.setData({singer: result});
    } else {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
    }
   },
    // 增加歌手浏览量
    async addSingerView(searchId) {
      const res = await myRequest({
        url: '/app/singer/addView',
        method: 'POST',
        data: {id: searchId}
      });
   },
   // 根据id获取专辑数据
   async getAlbumById(searchId) {
      const res = await myRequest({
        url: '/app/album/getById',
        method: 'POST',
        data: {id: searchId}
      });
      if(res.data.code === 0) {
          let result = res.data.data;
          result.coverPhoto = api.BASE_URL + '/photo/view?filename=' + result.coverPhoto;
          result.singerPhoto = api.BASE_URL + '/photo/view?filename=' + result.singerPhoto;
          this.setData({album: result});
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    },
    // 跳转音乐播放页面
    async goToPlayer(event) {
      const data = event.currentTarget.dataset
      const res = await myRequest({
        url: '/app/music/getMusicById',
        method: 'POST',
        data: {id: data.id}
      });
      if(res.data.code !== 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
      } else {
        if(this.properties.type === '1') {
          wx.navigateTo({
            url: '/pages/player/player?albumId='+ this.data.album.id +'&music=' + JSON.stringify(res.data.data),
          })
        } else if (this.properties.type === '2') {
          wx.navigateTo({
            url: '/pages/player/player?musicMenuId='+ this.data.musicMenu.id +'&music=' + JSON.stringify(res.data.data),
          })
        } else if (this.properties.type === '3') {
          wx.navigateTo({
            url: '/pages/player/player?singerId='+ this.data.singer.id +'&music=' + JSON.stringify(res.data.data),
          })
        }
      }
    }  

  }
})
